﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GanIndex
{
    public class LayJsonPageModel<T> {
        [JsonProperty("code")]
        public int Code { get; set; }
        [JsonProperty("msg")]
        public string  Msg { get; set; }
        [JsonProperty("count")]
        public int Count { get {
                return Data.Count;
            }
        }
        [JsonProperty("data")]
        public List<T> Data { get; set; }
    }
    public class BuyTopDataModel {
        [JsonProperty("id")]
        public string Id { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("quantity")]
        public int Quantity { get; set; }
        [JsonProperty("price")]
        public double Price { get; set; }
        [JsonProperty("sum")]
        public double Sum { get {
                return Quantity * Price;
            }
        }
        [JsonProperty("grossProfit")]
        public string GrossProfit { get; set; }
        [JsonProperty("Riqi")]
        public string riqi { get; set; }
        [JsonIgnore]
        public DateTime BuyTime { get; set; }
    }
    /// <summary>
    /// ordercustomerdata 的摘要说明
    /// </summary>
    public class buytop : IHttpHandler
    {
        internal static LayJsonPageModel<BuyTopDataModel> layJsonModel = new LayJsonPageModel<BuyTopDataModel>();
        public buytop()
        {
            
            List<BuyTopDataModel> data = new List<BuyTopDataModel>();
            for (var i = 0; i < 30; i++)
            {
                data.Add(new BuyTopDataModel() { Id = i.ToString(), Name = "abc", Quantity = new Random().Next(0, 100), Price = new Random().Next(0, 10), GrossProfit = "test", BuyTime = DateTime.Now.AddDays(i * (-1)) });
            }
            layJsonModel.Data = data;
            layJsonModel.Code = 0;
        }
        public void ProcessRequest(HttpContext context)
        {
            var startDate = context.Request["startDate"];
            var endDate = context.Request["endDate"];
            var page = context.Request["page"];
            var limit = context.Request["limit"];

            DateTime start, end;
            int pageSize =30;
            int pageIndex=1;
            if (startDate != null&&DateTime.TryParse(startDate,out start))
            {
                layJsonModel.Data = layJsonModel.Data.Where(s => s.BuyTime >= start).ToList();
            }
            if (endDate != null && DateTime.TryParse(endDate, out end))
            {
                layJsonModel.Data = layJsonModel.Data.Where(s => s.BuyTime <=end).ToList();
            }
            //if (page != null&&int.TryParse(page,out pageIndex)&&limit!=null&&int.TryParse(limit,out pageSize))
            //{
            //    layJsonModel.Data = layJsonModel.Data.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            //}
            var json= JsonConvert.SerializeObject(layJsonModel);


            context.Response.ContentType = "text/plain";
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}